package com.qifn.blog.Controller.Admin;

import com.github.pagehelper.PageInfo;
import com.qifn.blog.Model.Type;
import com.qifn.blog.Service.TypeService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import java.util.List;

@Controller
@RequestMapping("/admin")
public class AdminTypeController {

    @Resource
    TypeService typeService;

    @GetMapping("/type")
    public ModelAndView type(@RequestParam(defaultValue = "1") Integer pageNum){
        ModelAndView mv = new ModelAndView();
        PageInfo<Type> typePageInfo = typeService.queryAdminAllType(4,pageNum);
        mv.addObject("typePageInfo",typePageInfo);
        mv.setViewName("admin/classify");
        return mv;
    }

    @PostMapping("/insertType")
    public ModelAndView insertType(String typeName){
        Type type = new Type();
        ModelAndView mv = new ModelAndView();
        type.setName(typeName);
        boolean flag = typeService.insertType(type);
        if(flag){
            mv.addObject("message","分类添加成功");
        }else{
            mv.addObject("message","分类添加失败");
        }
        mv.setViewName("redirect:/admin/type");
        return mv;
    }

    @DeleteMapping("/deleteType")
    public ModelAndView deleteType(Integer typeId){
        ModelAndView mv = new ModelAndView();
        boolean flag = typeService.deleteTypeById(typeId);
        if(flag){
            mv.addObject("message","分类删除成功");
        }else {
            mv.addObject("message","分类删除失败");
        }
        mv.setViewName("redirect:/admin/type");
        return mv;
    }
}
